<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>添加用户</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../css/oksub.css">
    <script type="text/javascript" src="../../lib/loading/okLoading.js"></script>
</head>
<style>
    img{
        height: 150px;
        width: 150px;
    }
</style>
<body>
<div class="ok-body" id="app">
    <!--form表单-->
    <form class="layui-form layui-form-pane ok-form">
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
                <input type="text" name="username" v-model="username" placeholder="请输入用户名" autocomplete="off"
                       class="layui-input"
                       lay-verify="required">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">昵称</label>
            <div class="layui-input-block">
                <input type="text" name="nickname" v-model="nickname" placeholder="请输入昵称" autocomplete="off"
                       class="layui-input"
                       lay-verify="required">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-block">
                <input type="text" name="pwd" v-model="pwd" placeholder="请输入姓密码" autocomplete="off"
                       class="layui-input"
                       lay-verify="required">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">手机号码</label>
            <div class="layui-input-block">
                <input type="text" name="tel" v-model="tel" placeholder="请输入手机号码" autocomplete="off"
                       class="layui-input"
                       lay-verify="phone">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">头像</label>
            <div class="layui-input-block">
                <input type="file" name="file" id="file" placeholder="请选择图片" accept="image/*"
                       lay-verify="required">
            </div>
        </div>
        <div id="imageContainer">
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <input class="layui-btn" value="提交" @click="add">
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<!--js逻辑-->
<script src="../../lib/layui/layui.js"></script>
<script>
    layui.use(["element", "form", "laydate", "okLayer", "okUtils"], function () {
        let form = layui.form;
        let laydate = layui.laydate;
        let okLayer = layui.okLayer;
        let okUtils = layui.okUtils;

        okLoading.close();

        laydate.render({elem: "#birthday", type: "datetime"});

        form.verify({
            birthdayVerify: [/^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))(\s(([01]\d{1})|(2[0123])):([0-5]\d):([0-5]\d))?$/, '日期格式不正确']
        });
        // form.on("submit(add)", function (data) {
        //     okUtils.ajax("../../../admin/adminUser.action?op=addUser", "post", data.field, true).done(function (response) {
        //         console.log(response);
        //         okLayer.greenTickMsg("添加成功", function () {
        //             parent.layer.close(parent.layer.getFrameIndex(window.name));
        //         });
        //     }).fail(function (error) {
        //         console.log(error)
        //     });
        //     return false;
        // });
    });
</script>
<script src="axios.js"></script>
<script src="vue.js"></script>
<script>
    document.addEventListener('DOMContentLoaded', function () {
        const fileInput = document.getElementById('file');
        const imageContainer = document.getElementById('imageContainer');
        fileInput.addEventListener('change', function (event) {
            const file = event.target.files[0];
            if (file) {
                const reader = new FileReader();
                reader.onload = function (e) {
                    const img = document.createElement('img');
                    img.src = e.target.result;
                    imageContainer.innerHTML = '';
                    imageContainer.appendChild(img);
                };
                reader.readAsDataURL(file);
            } else {
                imageContainer.innerHTML = '';
            }
        });
    });
</script>
<script>
    var app = new Vue({
        el: "#app",
        data: {
            nickname: '',
            username: '',
            pwd: "",
            tel: "",
        },
        methods: {
            add() {
                var formDate=new FormData()
                const file=document.getElementById("file").files[0];
                formDate.append("nickname",this.nickname);
                formDate.append("username",this.username);
                formDate.append("pwd",this.pwd);
                formDate.append("tel",this.tel);
                formDate.append("file",file);
                axios.post("../../adminUser.action?op=addUser",formDate,{
                    headers: {
                        'Content-Type': 'multipart/form-data'
                    }
                }).then(result=>{
                    var jsonmodel=result.data;
                    if (jsonmodel.code==1){
                        alert("添加成功");
                        parent.layer.close(parent.layer.getFrameIndex(window.name))
                    }else {
                        alert("添加失败");
                    }
                })
            }
        }
    })
</script>
</body>
</html>
